package com.pay.controller;

import com.alipay.api.AlipayApiException;
import com.pay.model.interface_parameter.*;
import com.pay.service.inter.AlipayService;
import com.pay.utils.validate.annotation.PrintParameter;
import com.pay.utils.validate.annotation.ValidateParameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


/**
 * 支付宝支付
 *
 * @author MENG
 * @version 2018/7/4
 * @see
 */
@RestController
public class AlipayController
{

    Logger logger = LoggerFactory.getLogger(AlipayController.class);

    @Autowired
    private AlipayService alipayService;

    @RequestMapping(value = "getAliPayPcPagePay",method = RequestMethod.POST)
    @ValidateParameter
    @PrintParameter(description = "获取阿里支付PC跳转页面")
    public Map<String,Object> getAliPayPcPagePay(@RequestBody AliPayQrCodeParam aliPayQrCodeParam)
    {
        return alipayService.getAliPayPcPagePay(aliPayQrCodeParam.getAppId(),aliPayQrCodeParam.getPrivateKey(),
            aliPayQrCodeParam.getNotifyUrl(),
            aliPayQrCodeParam.getOrderNo(),aliPayQrCodeParam.getTotalAmount(),
            aliPayQrCodeParam.getSubject(),aliPayQrCodeParam.getQrCodeTimeoutExpress(),aliPayQrCodeParam.getReturnUrl());
    }


    /**
     * 获取支付宝支付生成的二维码
     *
     * 参数：AliPayQrCodeParam
     *
     * 返回：AliPayQrCodeReturn
     */
    @RequestMapping(value = "getAliPayQrCode",method = RequestMethod.POST)
    @ValidateParameter
    @PrintParameter(description = "获取阿里支付二维码")
    public Map<String,Object> getAliPayQrCode(@RequestBody AliPayQrCodeParam aliPayQrCodeParam)
    {
        return alipayService.scanPay(aliPayQrCodeParam.getAppId(),aliPayQrCodeParam.getPrivateKey(),
            aliPayQrCodeParam.getNotifyUrl(),
            aliPayQrCodeParam.getOrderNo(),aliPayQrCodeParam.getTotalAmount(),
            aliPayQrCodeParam.getSubject(),aliPayQrCodeParam.getQrCodeTimeoutExpress());
    }

    /**
     *  支付宝付款码支付
     *
     * 参数：AliPayAuthCodeParam
     *
     * 返回：AliPayAuthCodeReturn
     *
     */
    @RequestMapping("aliPayAuthCode")
    @ValidateParameter
    @PrintParameter(description = "支付宝付款码支付")
    public Map<String,Object> aliPayAuthCode(@RequestBody AliPayAuthCodeParam aliPayAuthCodeParam)
    {
        return alipayService.alipayCode(aliPayAuthCodeParam.getAppId(), aliPayAuthCodeParam.getPrivateKey(),
            aliPayAuthCodeParam.getNotifyUrl(),
            aliPayAuthCodeParam.getOrderNo(), aliPayAuthCodeParam.getAuthCode(),
            aliPayAuthCodeParam.getSubject(), aliPayAuthCodeParam.getTotalAmount(),
            aliPayAuthCodeParam.getTimeoutExpress());
    }

    /**
     *  H5 支付宝支付
     *
     * 参数：AliPayAuthCodeParam
     *
     * 返回：AliPayAuthCodeReturn 待测试
     *
     */
    @RequestMapping("aliPayMobileWeb")
    @ValidateParameter
    @PrintParameter(description = "手机H5支付宝支付")
    public Map<String,Object> aliPayMobileWeb(@RequestBody AlipayMobileWebParam alipayMobileWebParam)
    {
        return alipayService.alipayMobileWeb(alipayMobileWebParam.getAppId(), alipayMobileWebParam.getPrivateKey(),
            alipayMobileWebParam.getNotifyUrl(),
            alipayMobileWebParam.getOrderNo(),
            alipayMobileWebParam.getSubject(), alipayMobileWebParam.getTotalAmount(),
            alipayMobileWebParam.getTimeoutExpress(),alipayMobileWebParam.getReturnUrl());
    }

    /**
     * 支付宝退款
     *
     * 参数：AliPayRefundParam
     *
     * 返回：AliPayRefundReturn
     */
    @RequestMapping("aliPayRefund")
    @ValidateParameter
    @PrintParameter(description = "支付宝退款")
    public Map<String,Object> aliPayRefund(@RequestBody AliPayRefundParam aliPayRefundParam)
    {
        return alipayService.alipayRefund(aliPayRefundParam.getAppId(),aliPayRefundParam.getPrivateKey(),aliPayRefundParam.getOrderNo(),aliPayRefundParam.getRefundAmount(),aliPayRefundParam.getRefundReason());
    }

    /**
     * 支付宝退款
     *
     * 参数：AliPayRefundParam
     *
     * 返回：AliPayRefundReturn
     */
    @RequestMapping("aliPayRebate")
    @ValidateParameter
    @PrintParameter(description = "支付宝部分退款")
    public Map<String,Object> aliPayRebate(@RequestBody AliPayRebateParam aliPayRebateParam)
    {
        return alipayService.aliPayRebate(aliPayRebateParam.getAppId(),aliPayRebateParam.getPrivateKey(),aliPayRebateParam.getOrderNo(),aliPayRebateParam.getRefundAmount(),aliPayRebateParam.getRefundReason(),aliPayRebateParam.getTotalAmount(),aliPayRebateParam.getSunOrderCode());
    }

    /**
     * 支付宝 支付订单状态查询
     *
     * 参数：AliPayQueryParam
     *
     * 返回：AliPayQueryReturn
     *
     * @return
     */
    @RequestMapping("aliPayQuery")
    @ValidateParameter
    @PrintParameter(description = "支付订单状态查询")
    public Map<String,Object> aliPayQuery(@RequestBody AliPayQueryParam aliPayQueryParam)
        throws AlipayApiException
    {
        return alipayService.aliPayQuery(aliPayQueryParam);
    }


//    @Value("${update.order.url}")
//    private String updateOrderUrl;
//
//    @Value("${update.order.dubious.url}")
//    private String updateOrderDubiousUrl;
//
//
//    @RequestMapping("alipayNotify")
//    public void alipayNotify(HttpServletRequest request)
//    {
//
//        logger.info("收到支付宝支付通知。。。。");
//        //jie收通知和http请求
//        //获取支付宝POST过来反馈信息
//        Map<String,String> params = new HashMap<String,String>();
//
//        Map<String,String[]> requestParams = request.getParameterMap();
//
//        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();)
//        {
//            String name = (String) iter.next();
//
//            String[] values = (String[]) requestParams.get(name);
//
//            String valueStr = "";
//
//            for (int i = 0; i < values.length; i++)
//            {
//                valueStr = (i == values.length - 1) ? valueStr + values[i]
//                    : valueStr + values[i] + ",";
//            }
//
//            params.put(name, valueStr);
//        }
//
//        try
//        {
//            boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type); //调用SDK验证签名
//
//            logger.info("验证密钥："+signVerified);
//
//            if(signVerified) {//验证成功
//                //商户订单号
//                String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
//
//                //支付宝交易号
//                String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");
//
//                //交易状态
//                String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"),"UTF-8");
//
//                if ("TRADE_FINISHED".equals(trade_status) || "TRADE_SUCCESS".equals(trade_status))
//                {
//                    Map result = aliPayQuery(AlipayConfig.app_id,AlipayConfig.merchant_private_key,AlipayConfig.alipay_public_key,out_trade_no);
//
//                    //结果不是支付成功
//                    if(!PayStatusConstants.paySucess.code.equals(result.get(PayStatusConstants.codeKey)))
//                    {
//                        //标记订单可疑
//                        logger.info("发送 标记订单可疑 http请求");
//
//                        Map paramsMap = new HashMap<>();
//
//                        paramsMap.put("orderCode",out_trade_no);
//
//                        //执行http 调用 标记订单可疑
//                        HttpClientUtils.httpRequest(updateOrderDubiousUrl,"POST",paramsMap);
//
//                        logger.info("http请求发送成功");
//
//                    }
//                }
//            }
//            else
//            {
//                //验证失败
//                logger.error("阿里支付密钥验证失败fail！");
//            }
//        }
//        catch (AlipayApiException | UnsupportedEncodingException e)
//        {
//            logger.error("阿里支付密钥验证执行错误!",e);
//        }
//    }


}
